package com.myplaylist.server;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

import com.myplaylist.utils.Constants;

public class UdpServerIPHandler implements Runnable {

	private DatagramSocket datagramSocket;

	@Override
	public void run() {
		try {
			System.out.println("UDP service is up");
			datagramSocket = new DatagramSocket(Constants.UDP_SERVER_PORT_LISTEN);
			
			while (true) {
				try {
					byte[] buffer = new byte[1024];
					DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
					datagramSocket.receive(packet);
					System.out.println("Received packet, starting UDP handler thread.");
					new UdpConnectionHandlerThread(packet).start();
					
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
			
		} catch (IOException e) {
			System.err.println("IOException: ");
			e.printStackTrace();
		}
	}

}
